package com.jeesite.modules.sys.web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.jeesite.modules.edu.entity.EduTeacherTitle;
import com.jeesite.modules.qr.LogoConfig;
import com.jeesite.modules.sys.entity.Employee;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.EmployeeService;
import com.jeesite.modules.sys.service.UserService;
@Controller
@RequestMapping(value = "${adminPath}/sys/qr")
@ConditionalOnProperty(name = "web.core.enabled", havingValue = "true", matchIfMissing = true)
public class QrController {
	@Autowired
	private UserService userService;
	@Autowired
	private EmployeeService employeeService;
	//统计教师数
	@RequestMapping(value = "code")
	public void totalTeacher(HttpServletRequest request, HttpServletResponse response) throws WriterException, IOException {
		String uid = request.getParameter("uid");
		if(StringUtils.isEmpty(uid)) {
			throw new RuntimeException("参数异常");
		}
		sun.misc.BASE64Decoder decoder = new sun.misc.BASE64Decoder();
		String userCode = new String(decoder.decodeBuffer(uid));
		User user = userService.get(userCode);
		
		if(StringUtils.isEmpty(user)) {
			throw new RuntimeException("参数异常");
		}
		
        OutputStream os = response.getOutputStream();
        response.reset();
        response.setContentType("image/jpg");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);

        Hashtable hints = new Hashtable();
        Employee employee = employeeService.get(userCode);
        String title = "暂无职称";
        EduTeacherTitle eduTeacherTitle = employee.getEduTeacherTitle();
        if(!StringUtils.isEmpty(eduTeacherTitle)) {
        	title = eduTeacherTitle.getTeacherName();
        }
        String codeUrl = "BEGIN:VCARD\n" +
			    "VERSION:3.0\n" +
			    "N:"+user.getUserName()+"\n" +
			    "EMAIL:"+user.getEmail()+"\n" +
			    "TEL:"+user.getMobile()+"\n" +
			    "ORG:"+employee.getOffice().getOfficeName()+"\n" +
			    "TITLE:"+title+"\n" +
			    "URL:http://hljgxrc.it.greathiit.com/xxxt\n" +
			    "NOTE:黑龙江省普通高等学校人才信息系统V1.0\n" +
			    "END:VCARD";
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(codeUrl, BarcodeFormat.QR_CODE, 400, 400, hints);
        
        BufferedImage image = toBufferedImage(bitMatrix);
		LogoConfig logoConfig = new LogoConfig();
		image = logoConfig.LogoMatrix(image);

		ImageIO.write(image, "jpg", os);
		os.flush();
		os.close();
	}
	
	private static final int BLACK = 0xFF000000;// 用于设置图案的颜色
	private static final int WHITE = 0xFFFFFFFF; // 用于背景色

	public static BufferedImage toBufferedImage(BitMatrix matrix) {
		int width = matrix.getWidth();
		int height = matrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, (matrix.get(x, y) ? BLACK : WHITE));
			}
		}
		return image;
	}
	
}
